//-----------------------------------------------------------------------------
//クラス名：Club.java
//所    属：H3A1 04
//名    前：大瀬碩
//説    明：クラブ情報を取得し表示させる
//-----------------------------------------------------------------------------
package club;


import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


//@WebServlet("/Club")
public class Club extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

    public Club() {
        super();

    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
	 	
	 	HttpSession session = request.getSession(true);
	 	
	 	//DB接続
	 	ServletContext context = this.getServletContext();
		String path = context.getRealPath("/WEB-INF/connect.properties");
		
		InputStream in = new FileInputStream(path);
		Properties properties = new Properties();
		properties.load(in);
		in.close();
	    
	    Connection conn = null;
	    String url = properties.getProperty("key1");
	    String user = properties.getProperty("key2");
	    String password = properties.getProperty("key3");
	    
	    String i = request.getParameter("club");
	    session.setAttribute("sel_club", i);
	    
	    try {
	      Class.forName("com.mysql.jdbc.Driver").newInstance();
	      conn = DriverManager.getConnection(url, user, password);
	          
	          //DBから情報取得し表示させる
		      Statement stmt = conn.createStatement();
		      String sql = "SELECT * FROM club WHERE club_id = "+i;
		      ResultSet rs = stmt.executeQuery(sql);
		      
		      while(rs.next()){
		    	  request.setAttribute("at",rs.getString("activity_time"));
		    	  request.setAttribute("ap",rs.getString("activity_place"));
		    	  request.setAttribute("ac",rs.getString("activities"));
		      }
	      rs.close();
	      stmt.close();
	    }catch (ClassNotFoundException e){
	    	
	    }catch (SQLException e){
	      
	    }catch (Exception e){
	    	
	    }finally{
	      try{
	        if (conn != null){
	          conn.close();
	        }
	      }catch (SQLException e){
	        
	      }
	    }
	    
	    RequestDispatcher rd = request.getRequestDispatcher("Club.jsp");
		rd.forward(request,response);
	}

}
